extends Node2D
@onready var camera_2d: Camera2D = $Camera2D

var num = 0

func _ready() -> void:
	await get_tree().create_timer(0.5).timeout
	$CanvasLayer/Label.hide()

func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("grab"):
		$StaticBody2D/Sprite2D/GrabNode2DComponent.stop_grab()

func _on_down_area_area_entered(area: Area2D) -> void:
	if area is CarBullet:
		var temp_node2d:Node2D = area.passenger
		area._recycle_self()
		await get_tree().create_timer(0.5).timeout
		temp_node2d.position = Vector2(-196 + 496 * num,-100)

func _on_down_area_body_entered(body: Node2D) -> void:
	if body is RigidBody2D:
		body.apply_central_impulse(Vector2.LEFT * 10 + Vector2.UP * 200)
	else:
		body.position = Vector2(-196 + 496 * num,-100)

func _on_from_1_to_2_body_entered(body: Node2D) -> void:
	if body is Player:
		if body.position.x < $from1to2.position.x:
			num = 1
			camera_2d.position.x = 496 * num
		else:
			num = 0
			camera_2d.position.x = 496 * num

func _on_from_1_to_2_body_exited(body: Node2D) -> void:
	if body is Player:
		if body.position.x > $from1to2.position.x:
			num = 1
			camera_2d.position.x = 496 * num
		else:
			num = 0
			camera_2d.position.x = 496 * num


func _on_from_2_to_3_body_entered(body: Node2D) -> void:
	if body is Player:
		if body.position.x < $from2to3.position.x:
			num = 2
			camera_2d.position.x = 496 * num
		else:
			num = 1
			camera_2d.position.x = 496 * num

func _on_from_2_to_3_body_exited(body: Node2D) -> void:
	if body is Player:
		if body.position.x > $from2to3.position.x:
			num = 2
			camera_2d.position.x = 496 * num
		else:
			num = 1
			camera_2d.position.x = 496 * num


func _on_door_button_body_entered(body: Node2D) -> void:
	if body is TileMapLayer:
		return
	for i in range(68,79):
		$TileMapLayer.erase_cell(Vector2(i,0))

func _on_door_button_body_exited(body: Node2D) -> void:
	if body is TileMapLayer:
		return
	for i in range(68,79):
		$TileMapLayer.set_cell(Vector2(i,0),0,Vector2(0,0))

func _on_from_3_to_4_body_entered(body: Node2D) -> void:
	if body is Player:
		if body.position.x < $from3to4.position.x:
			num = 3
			camera_2d.position.x = 496 * num
		else:
			num = 2
			camera_2d.position.x = 496 * num


func _on_from_3_to_4_body_exited(body: Node2D) -> void:
	if body is Player:
		if body.position.x > $from3to4.position.x:
			num = 3
			camera_2d.position.x = 496 * num
		else:
			num = 2
			camera_2d.position.x = 496 * num


func _on_button_button_down() -> void:
	get_tree().change_scene_to_file("res://GDBase/SceneBase/start_scene.tscn")


func _on_left_button_down() -> void:
	$CanvasLayer/Left.hide()
